package cn.haut.online_read.controller.crawler.zhuishushenqi;

import cn.haut.online_read.crawler.CategoryCrawler;
import cn.haut.online_read.utils.CrawlerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author xing
 * @date 2021-04-20 21:28
 */
@Controller("categoryController")
@RequestMapping
@Slf4j
public class CategoryController {

    public static final String BASE_URL_CATEGORY = "http://zhuishushenqi.com/category";

    /**
     * 获取页面 json 数据
     *
     * @param gender
     * @param type
     * @param major
     * @param minor
     * @return
     */
    @GetMapping(value = {"/category"})
    @ResponseBody
    public String getBooklist(
            @RequestParam(value = "gender", required = false) String gender,
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "major", required = false) String major,
            @RequestParam(value = "minor", required = false) String minor,
            @RequestParam(value = "currentPage", required = false) String currentPage
    ) {
        String url = "";
        // gender 男生，女生，出版物
        if (gender == null) {
            url = BASE_URL_CATEGORY;
        }
        // type 热门，新书，好评，完结，VIP
        else if (type == null) {
            url = String.format("%s?gender=%s", BASE_URL_CATEGORY, gender);
        }
        // major 玄幻，科幻，现在言情，青春校园
        else if (major == null) {
            url = String.format("%s?gender=%s&type=%s", BASE_URL_CATEGORY, gender, type);
        }
        // minor 东方玄幻，未来世界，豪门总裁，浪漫校园
        else if (minor == null) {
            url = String.format("%s?gender=%s&type=%s&major=%s", BASE_URL_CATEGORY, gender, type, major);
        } else {
            url = String.format("%s?gender=%s&type=%s&major=%s&minor=%s", BASE_URL_CATEGORY, gender, type, major, minor);
        }
        if(currentPage != null) {
            if(url.equals(BASE_URL_CATEGORY)) {
                url = url + "?page=" + currentPage;
            } else {
                url = url + "&page=" + currentPage;
            }
        }
        return CrawlerUtils.getJsonResult(new CategoryCrawler(), url);
    }

    /**
     * 获取页面的链接
     *
     * @param gender
     * @param type
     * @param major
     * @param minor
     * @return
     */
    private String getUrl(String gender, String type, String major, String minor) {
        return String.format("%s?gender=%s&type=%s&major=%s&minor=%s", BASE_URL_CATEGORY, gender, type, major, minor);
    }

}
